
const app = getApp();
const wxApi = app.globalData.wxApi;
const Api = app.globalData.Api;

const WORD_STUDY_PAGE = "../../index/topic-detail/topic-detail"
Page({
  data: {
    words: [],
    loadMoreType: 0,
    infoviewShow: false
  },
  onLoad() {
    this.pageNo = 1;
    this.init();
  },
  init() {
    wxApi.setNavTitle('我的收藏');
    this.refresh();
  },
  refresh() {
    let self = this;
    this.pageNo = 1;
    this.loadCollectedWords(this.pageNo, data => {
      console.log(data);
      self.setData({
        words: data,
        infoviewShow: !data.length
      })
      console.log( self.data.infoviewShow );
    })
  },
  onPullDownRefresh() {
    console.log( '下拉刷新' );
    this.refresh();
  },
  onReachBottom() {
    console.log( '加载更多' );
    this.loadMore();
  },
  loadMore() {
    let self = this;
    let words = this.data.words;
    this.pageNo ++;
    console.log( this.pageNo );
    this.setData({
      loadMoreType: 1 // 显示加载更多
    })
    this.loadCollectedWords(this.pageNo, data => {
      if ( data.length ) {
        words = words.concat( data );
        self.setData({
          loadMoreType: 1, // 显示加载更多
          words: words
        })
      } else {
        self.pageNo --; //没有更多则页码减一
        self.setData({
          loadMoreType: 2 // 显示无更多
        })
      }
    })
  },
  loadCollectedWords(pageNo, callback) {
    pageNo = pageNo || 1;
    let params = {
      pageNo: pageNo,
      pageSize: 20
    }
    Api.getCollectedTopics(params, res => {
      console.log(res);
      callback( res.records );
    } )
  },
  gotoStudyWord( e ) {
    let word = e.currentTarget.dataset.word;
    let index = e.currentTarget.dataset.index;
    let params = {
      id: word.domainId,
      subed: word.textbookSubscribed,
      canSwitch: 'no'
    }
    console.log(params);
    wxApi.navigateTo(WORD_STUDY_PAGE, params);
  }
})